import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STData, STPage, STRes } from '@delon/abc/st';
import { SFComponent, SFSchema, SFSelectWidgetSchema } from '@delon/form';
import { ModalHelper, _HttpClient, SettingsService } from '@delon/theme';
import { ModalHelperOptions } from '@delon/theme/src/services/modal/modal.helper';
import format from 'date-fns/format';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { BusinessOrderOrderProcessComponent } from '../../business/order/order-process/order-process.component';
import { ConfHostEditComponent } from './edit/edit.component';
import { ConfHostStateComponent } from './state/state.component';

@Component({
  selector: 'app-conf-host',
  templateUrl: './host.component.html'
})
export class ConfHostComponent implements OnInit {
  url = `api/hosts`;
  isAdmin = false;
  isShow = false;
  company_id = 0;
  @ViewChild('sf', { static: false }) private sf!: SFComponent;
  searchSchema: SFSchema = {
    properties: {
      keyword: {
        type: 'string',
        title: '',
        ui: {
          placeholder: '搜索主机信息'
        }
      },
      account_username: {
        type: 'string',
        title: '',
        ui: {
          placeholder: '关联店铺账号'
        }
      }
    }
  };
  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    { title: '服务器ip', index: 'host_ip', className: 'text-center' },
    { title: '服务器端口', index: 'host_port', className: 'text-center' },
    { title: '服务器登录用户名', index: 'host_username', className: 'text-center' },
    { title: '关联店铺账号', index: 'account_username', className: 'text-center', default: '-' },
    {
      title: '主机状态',
      index: 'host_state',
      type: 'badge',
      badge: {
        0: { text: '未运行', color: 'error' },
        1: { text: '运行中', color: 'success' }
      },
      className: 'text-center',
      colSpan: 10
    },
    {
      title: '部署状态',
      index: 'release_status',
      type: 'badge',
      badge: {
        0: { text: '未部署', color: 'error' },
        1: { text: '已部署', color: 'success' }
      },
      className: 'text-center',
      colSpan: 10
    },
    {
      title: '部署日期',
      index: 'release_date',
      format: (item, _col, index) => format(new Date(item['release_date']), 'yyyy-MM-dd').replace('0001-01-01', '-'),
      className: 'text-center',
      default: '-'
    },
    {
      title: '状态',
      index: 'status',
      type: 'badge',
      badge: {
        0: { text: '禁用', color: 'error' },
        1: { text: '启用', color: 'success' }
      },
      className: 'text-center',
      colSpan: 10
    },
    {
      title: '操作',
      buttons: [
        {
          text: '主机状态',
          type: 'none',
          icon: 'edit',
          className: 'text-success',
          click: (item: any) => this.hostStates(item.ID, item.host_ip, item.account_username)
        },
        {
          text: '编辑',
          icon: 'edit',
          type: 'static',
          modal: {
            component: ConfHostEditComponent,
            params: (record: STData) => record,
            modalOptions: { nzMaskClosable: false }
          },
          click: 'reload'
        },
        { text: '删除', type: 'del', icon: 'delete', className: 'text-error', click: (item: any) => this.del(item.ID) }
      ],
      className: 'text-center'
    }
  ];

  res: STRes = {
    reName: {
      total: 'data.total',
      list: 'data.list'
    }
  };

  paginate: STPage = {
    showSize: true
  };

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private msgSrv: NzMessageService,
    private settingService: SettingsService
  ) {
    this.company_id = this.settingService.user.company_id;
    //console.log(this.settingService.user.ID);
    if (this.settingService.user.ID == 1) {
      this.isAdmin = true;
    }
  }

  ngOnInit = (): void => {};

  add(): void {
    this.modal.createStatic(ConfHostEditComponent, { i: { id: 0 } }).subscribe(() => this.st.reload());
  }

  /**
   * 删除
   *
   * @param id 数据id
   */
  del(id: number): void {
    this.http.delete(`api/host/${id}`).subscribe(() => {
      this.st.reload();
    });
  }

  // 主机状态详情
  hostStates(host_id: number, host_ip: string, account_username: string): void {
    let options: ModalHelperOptions = {
      size: 'md'
    };
    this.modal
      .createStatic(ConfHostStateComponent, { i: { ID: host_id, host_ip: host_ip, account_username: account_username } }, options)
      .subscribe(() => {
        this.st.reload();
      });
  }
}
